package com.chenjun.designpattern.structural.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class 动态代理模式2
{
	public static void main(String[] args)
	{
		IUserDao userDaoProxy = (IUserDao)Proxy.newProxyInstance(IUserDao.class.getClassLoader(), new Class[]{IUserDao.class}, new InvocationHandler()
		{
			@Override
			public Object invoke(Object target, Method method, Object[] args) throws Throwable
			{
				target = new UserDao();
				return method.invoke(target, args);
			}
		});
		Long result = userDaoProxy.queryByName("aaa");
		System.out.println("result = "+result);
	}
}

interface IUserDao
{
	public Long queryByName(String name);

}

class UserDao implements IUserDao
{
	public Long queryByName(String name)
	{
		return (long) name.length();
	}
}
